import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

public class Katamino {

	private JFrame frame;
	private Jeu2 jeu;
	private JButton restart;
	private JTextField caseTab;
	private int taille;
	private JPanel p;

	public Katamino() {
		//createModel();
		createView();
		placeComponents();
		createControler();
	}

	public void display() {
		frame.pack();
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
	}

	public void createModel(int taille) {
		jeu = new Jeu2(taille);
	}

	public void createView() {
		frame = new JFrame("Katamino");
		frame.setResizable(false);
		restart = new JButton("Recommencer");
		caseTab = new JTextField(10);
	}

	public void placeComponents() {
		p = new JPanel(); {
			p.add(caseTab);
			p.add(new JLabel("Nombre de case du tableau entre 4 et 12"));
		}
		frame.add(p);
	}

	public void createControler() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		restart.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//createModel();
			}
		});

		caseTab.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				String text = ((JTextField) e.getSource()).getText();
				taille = Integer.parseInt(text);
				
				if (taille >= 4 && taille <= 12) {
					caseTab.setFocusable(false);

					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							frame.remove(p);
							p.setVisible(false);

							createModel(taille);
							frame.add(jeu);
							jeu.requestFocus();
							display();
						}
					});
				}
			}
		});
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				new Katamino().display();
			}
		});
	}
}